<link rel="stylesheet" href="/assets/home/plugins/mescroll/mescroll.min.css">
<link rel="stylesheet" href="/assets/home/css/teacher_list.css" />

<style>
    .mescroll-totop{
        right: 30px;
        bottom: 60px;
    }
</style>
<!-- 搜索框 -->
<div class="mui-input-row mui-search">
    <input type="search" class="mui-input-clear" placeholder="请输入昵称" name="search" />
</div>

<div id="teacher" class="mescroll">
    <div class="list">
    </div>
</div>

<!-- 引入底部 -->
{include file='common/footer'/}

<script src="/assets/home/plugins/mescroll/mescroll.min.js"></script>

<script>

    // 关键字
    let search = '';

    // 实例化插件
    let meScroll = new MeScroll('teacher', {
        // 下拉刷新
        down: {
            auto: false,//是否在初始化完毕之后自动执行下拉回调callback; 默认true
            callback: downCallback // 下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        },
        // 上拉加载
        up: {
            auto: true, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
            callback: UpCallback, //上拉回调,此处可简写; 相当于 callback: function (page) { upCallback(page); }
            toTop:{ //配置回到顶部按钮
                duration: 1000,
                src : "/assets/home/images/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
                offset : 100
            }
        }
    });

    async function downCallback() {
        meScroll.resetUpScroll();

        $('#teacher .list').empty();

        return false;
    }

    async function UpCallback(page) {
        let data = {
            page: page.num,
            limit: page.size,
            search
        }

        let result = await POST({
            url: 'subject/teacher/index',
            params: data
        });

        if (result.code === 1) {
            let list = result.data.list;
            let count = result.data.count;

            meScroll.endBySize(list.length, count);   //满足条件后不在上拉刷新

            fetchList(list);
        }else{
            meScroll.endBySize(0, 0);
            mui.toast(result.msg);
            return false;
        }
    }

    function fetchList(list = []) {
        if (list.length > 0) {
            let Item = '';
            for (let item of list) {
                Item = `
                <div class="item">
                    <a href="{:url('/home/subject/teacher/info')}?teacherid=${item.id}">
                        <div class="avatar">
                            <img src="${item.avatar_cdn}" alt="">
                        </div>
                        <div class="info">
                            <h5 class="title">${item.name}</h5>
                            <p class="course">课程：${item.subject_count}</p>
                            <p class="job">职位：${item.job}</p>
                            <p class="content">简介：${item.content}</p>
                            <div class="follow">
                                <img src="/assets/home/images/like.png" alt="">
                                <span>${item.follow_count}</span>
                            </div>
                        </div>
                    </a>
                </div>
                `
                $('#teacher .list').append(Item);
            }
        }
    }

    $('.mui-search input[type="search"]').keydown(function (e) {
        if (e.keyCode == 13 || e.keyCode == 108) {
            search = $(this).val();

            downCallback();

            return false;
        }
    })

</script>